package com.example.phone.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabHostActivitys extends TabActivity implements
		OnCheckedChangeListener {
	/** Called when the activity is first created. */

	private RadioGroup group;
	private TabHost tabHost;
	public static final String TAB_ITEM_1 = "tabItem1";

	public static final String TAB_ITEM_2 = "tabItem2";

	public static final String TAB_ITEM_3 = "tabItem3";

	public static final String TAB_ITEM_4 = "tabItem4";
	public static final String TAB_ITEM_5 = "tabItem5";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabhost);

		group = (RadioGroup) findViewById(R.id.main_radio);
		group.setOnCheckedChangeListener(TabHostActivitys.this);

		tabHost = this.getTabHost();
		TabSpec tab1 = tabHost.newTabSpec(TAB_ITEM_1);
		TabSpec tab2 = tabHost.newTabSpec(TAB_ITEM_2);
		TabSpec tab3 = tabHost.newTabSpec(TAB_ITEM_3);
		TabSpec tab4 = tabHost.newTabSpec(TAB_ITEM_4);
		TabSpec tab5 = tabHost.newTabSpec(TAB_ITEM_5);

		tab1.setIndicator(TAB_ITEM_1).setContent(
				new Intent(this, PictureShowActivity.class));
		tab2.setIndicator(TAB_ITEM_2).setContent(
				new Intent(this, LocationOverlayDemo.class));
		tab3.setIndicator(TAB_ITEM_3).setContent(
				new Intent(this, ListBusActivity.class));
		tab4.setIndicator(TAB_ITEM_4).setContent(
				new Intent(this, SeekActivity.class));
		tab5.setIndicator(TAB_ITEM_5).setContent(
				new Intent(this, LoginActivity.class));

		tabHost.addTab(tab1);
		tabHost.addTab(tab2);
		tabHost.addTab(tab3);
		tabHost.addTab(tab4);
		tabHost.addTab(tab5);
		group.check(R.id.radio_button2);
		tabHost.setCurrentTabByTag(TAB_ITEM_2);

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub

		switch (checkedId) {

		case R.id.radio_button1:
			tabHost.setCurrentTabByTag(TAB_ITEM_1);
			break;
		case R.id.radio_button2:
			tabHost.setCurrentTabByTag(TAB_ITEM_2);
			break;
		case R.id.radio_button3:
			tabHost.setCurrentTabByTag(TAB_ITEM_3);
			break;
		case R.id.radio_button4:
			tabHost.setCurrentTabByTag(TAB_ITEM_4);
			break;
		case R.id.radio_button5:
			tabHost.setCurrentTabByTag(TAB_ITEM_5);
			break;
		default:
			break;
		}
	}

	@Override
	public void onStop() {

		super.onDestroy();
	}

	@Override
	public void onDestroy() {

		super.onDestroy();
	}

	protected void dialog() {
		AlertDialog.Builder builder = new Builder(TabHostActivitys.this);
		builder.setMessage("确认退出吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("确认", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				TabHostActivitys.this.finish();
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			dialog();
		}
		return false;
	}

}